package com.huike.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huike.business.domain.TbBusiness;
import com.huike.business.domain.TbBusinessTrackRecord;
import com.huike.business.domain.vo.BusinessTrackVo;
import com.huike.business.domain.vo.TbBusinessVO;
import com.huike.business.mapper.TbBusinessMapper;
import com.huike.business.service.TbBusinessTrackRecordService;
import com.huike.business.mapper.TbBusinessTrackRecordMapper;
import com.huike.common.constant.Constants;
import com.huike.common.constant.ErrorMessageConstants;
import com.huike.common.exception.BaseException;
import com.huike.common.utils.SecurityUtils;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.Date;

/**
* @author 93238
* @description 针对表【tb_business_track_record(商机跟进记录)】的数据库操作Service实现
* @createDate 2023-10-12 06:08:37
*/
@Service
@RequiredArgsConstructor
public class TbBusinessTrackRecordServiceImpl extends ServiceImpl<TbBusinessTrackRecordMapper, TbBusinessTrackRecord>
    implements TbBusinessTrackRecordService{
    private final TbBusinessTrackRecordMapper tbBusinessTrackRecordMapper;

    private final TbBusinessMapper tbBusinessMapper;
    /**
     * 新增商机跟进记录
     * @param businessTrackVo
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(BusinessTrackVo businessTrackVo) {
        if (ObjectUtils.isEmpty(businessTrackVo)){
            throw new BaseException(ErrorMessageConstants.BUSINESS_TRACK_RECORD_DATA_ERROR);
        }
        TbBusinessTrackRecord tbBusinessTrackRecord = new TbBusinessTrackRecord();
        //复制相关信息到商机跟进记录对象
        BeanUtils.copyProperties(businessTrackVo,tbBusinessTrackRecord);
        tbBusinessTrackRecord.setCreateBy(SecurityUtils.getUsername());
        tbBusinessTrackRecord.setCreateTime(new Date());
        // 新增商机跟进记录
        tbBusinessTrackRecordMapper.insert(tbBusinessTrackRecord);

        //复制相关信息到商机对象
        TbBusiness tbBusiness = TbBusiness.builder().build();
        BeanUtils.copyProperties(businessTrackVo,tbBusiness);
        tbBusiness.setLastUpdateTime(new Date());

        LambdaQueryWrapper<TbBusiness> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TbBusiness::getId,businessTrackVo.getBusinessId());
        //更新商机表
        tbBusinessMapper.update(tbBusiness,wrapper);
    }
}




